package com.example.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.example.domain.ExportXsEntity;
import com.example.domain.XsBkhrEntity;
import com.example.domain.XsEntity;
import com.example.domain.XsHkEntity;
import com.example.domain.XsHtEntity;
import com.example.domain.XsRbEntity;
import com.example.domain.XsTbEntity;
import com.example.domain.XsTjEntity;
import com.example.domain.XsWQEntity;
import com.example.domain.XsXmEntity;
import com.example.mapper.DeptMapper;
import com.example.mapper.XsMapper;
import com.example.service.XsService;
import com.example.service.XsWQService;
import com.example.utils.DateUtils;
import com.example.utils.config.CommonConfig;
import com.example.utils.poi.ExcelUtil;

import lombok.extern.slf4j.Slf4j;

@Service
@Transactional
@Slf4j
public class XsServiceImp implements XsService {

	@Autowired
	XsMapper xsMapper;
	
	@Override
	public List<XsEntity> queryXsUserIncludeData(String year) {
		String deptNamesStr = CommonConfig.getDeptNames();
		log.info("配置文件配置部门信息:{}",deptNamesStr);
		String[] deptNames = deptNamesStr.split(",");
		List<XsEntity> personList = xsMapper.queryXsPerson(deptNames);
//		log.info("查询到相关部门人员数量:{},信息:{}",personList.size(),personList);
		
		//被考核人，需要考核日报的人员
		List<XsBkhrEntity> bkhrList = xsMapper.queryXsBkhr();
		// 合同
		List<XsHtEntity> htlist = xsMapper.queryXsHt(year,deptNames);
		// 项目
		List<XsXmEntity> xmlist = xsMapper.queryXsXm(year,deptNames);
		// 日报
		List<XsRbEntity> rblist = xsMapper.queryXsRb(year,deptNames);
		// 投标
		List<XsTbEntity> tblist = xsMapper.queryXsTb(year,deptNames);
		// 回款
		List<XsHkEntity> hklist = xsMapper.queryXsHk();
		// 2022.3.30 外勤打卡数据，用来计算未出差的天数
		int yccDays = queryWorkDaysByYear(year);
		List<XsWQEntity> wqList = getFinallist(year, yccDays);
		Map<String, Integer> wqMap = new HashMap<String, Integer>();
		for (XsWQEntity xsWQEntity : wqList) {
			int days = xsWQEntity.getYcqts() - xsWQEntity.getCcDays();
			if(days < 0) {
				days = 0;
			}
			wqMap.put(xsWQEntity.getId(), days);
		}
		
//		log.info("===============开始时间"+DateUtils.getTimeStr());
		for (XsEntity person : personList) {
			calcXsData_bak(person,htlist,xmlist,rblist,tblist,hklist,year,bkhrList, wqMap,yccDays);
		}
//		log.info("===============结束时间"+DateUtils.getTimeStr());
//		for (XsEntity person : personList) {
//			XsTjEntity tjEntity = person.getXsTjEntity()==null?new XsTjEntity():person.getXsTjEntity();
//			int kqCount = xsMapper.queryKQ(person.getLoginid(),year);
//			tjEntity.setKhf(kqCount);
//			person.setXsTjEntity(tjEntity);// 先每个人查询考勤数
//		}
//		calcXsData(personList,htlist,xmlist,rblist,tblist,hklist,year);
//		// 将未写日志的数量置为负数
//		for (XsEntity person : personList) {
//			XsTjEntity tjEntity = person.getXsTjEntity();
//			tjEntity.setKhf(0-tjEntity.getKhf());
//			person.setXsTjEntity(tjEntity);
//		}
//		log.info("---------------结束时间"+DateUtils.getTimeStr());
		
		return personList;
	}
    
	@Override
	public List<XsEntity> tj(String year) {
		List<XsEntity> retList = new ArrayList<XsEntity>();
		List<XsEntity> personList = queryXsUserIncludeData(year);
		Map<String,XsEntity> map = new HashMap<String,XsEntity>();
		for (XsEntity xs : personList) {
			String deptName = xs.getDeptname();
			if(map.get(deptName) == null) {
				XsEntity entity = new XsEntity();
				entity.setDeptname(deptName);
				entity.setXsTjEntity(xs.getXsTjEntity());
				entity.setLastname(deptName);
				entity.setLoginid("");
				entity.setUserid("");
				entity.setCompanystartdate("");
				map.put(deptName, entity);
			}else {
				XsEntity xsFinal = map.get(deptName);
				XsTjEntity xstjFinal = sumTj(xsFinal.getXsTjEntity(),xs.getXsTjEntity());
				xsFinal.setXsTjEntity(xstjFinal);
				map.put(deptName, xsFinal);
			}
		}
//		List<XsEntity> tjMapList = map.values().stream().collect(Collectors.toList());
		for (XsEntity value : map.values()) {
			retList.add(value);
			for (XsEntity xsEntity : personList) {
				if(xsEntity.getDeptname().equals(value.getDeptname())) {
					retList.add(xsEntity);
				}
			}
		}
		return retList;
	}
	
	@Override
	public String export(String year,String path) {
		List<ExportXsEntity> retList = export(year);
        ExcelUtil<ExportXsEntity> util = new ExcelUtil<ExportXsEntity>(ExportXsEntity.class);
        String fileName = util.exportExcel(retList, "销售考核导出"+year, path);
        log.info("excel文件名称:"+CommonConfig.getProfile()+fileName);
		return "/excel/"+fileName;
	}
	
	@Override
	public List<ExportXsEntity> export(String year) {
		List<ExportXsEntity> retList = new ArrayList<ExportXsEntity>();
		
		List<XsEntity> personList = queryXsUserIncludeData(year);
		Map<String,XsEntity> map = new HashMap<String,XsEntity>();
		for (XsEntity xs : personList) {
			String deptName = xs.getDeptname();
			if(map.get(deptName) == null) {
				XsEntity entity = new XsEntity();
				entity.setDeptname(deptName);
				entity.setXsTjEntity(xs.getXsTjEntity());
				entity.setLastname(deptName);
				entity.setLoginid("");
				entity.setUserid("");
				entity.setCompanystartdate("");
				map.put(deptName, entity);
			}else {
				XsEntity xsFinal = map.get(deptName);
				XsTjEntity xstjFinal = sumTj(xsFinal.getXsTjEntity(),xs.getXsTjEntity());
				xsFinal.setXsTjEntity(xstjFinal);
				map.put(deptName, xsFinal);
			}
		}
		int i = 1;
		ExportXsEntity tongji = new ExportXsEntity();
		for (XsEntity value : map.values()) {
			ExportXsEntity exportParentXsEntity = changeData2ExportData(value,i+"");
			sumHj(tongji,exportParentXsEntity);
//			tongji.setHtpre(tongji.getHtpre() + exportParentXsEntity.getHtpre());
			retList.add(exportParentXsEntity);
			int j = 1;
			for (XsEntity xsEntity : personList) {
				if(xsEntity.getDeptname().equals(value.getDeptname())) {
					ExportXsEntity exportXsEntity = changeData2ExportData(xsEntity,i+"."+j);
					j++;
					retList.add(exportXsEntity);
				}
			}
			i++;
		}
		if(tongji.getHte() == 0) {
			tongji.setHkl(0);
		}else {
			tongji.setHkl(tongji.getHtcur()/tongji.getHte());
		}
		tongji.setId(i+"");
		tongji.setDeptname("合计");
		retList.add(tongji);// 最终的统计
        return JSONObject.parseArray(JSONObject.toJSONString(retList),ExportXsEntity.class);
//		return retList;
	}
	
	private void calcXsData(List<XsEntity> personList,
			List<XsHtEntity> htList,List<XsXmEntity> xmList,List<XsRbEntity> rbList,
			List<XsTbEntity> tbList,List<XsHkEntity> hklist,
			String year){
		
		// 合同
		// 循环 将合同分成1-4季度 金额分别累加起来
		for (XsHtEntity ht : htList) {
			for (XsEntity person : personList) {
				if(person.getUserid().equals(ht.getSqr())) {
					XsTjEntity tjEntity = person.getXsTjEntity();
					if(DateUtils.compare2DateString(ht.getQdrq(), DateUtils.getQ1thDateStr())<=0) {
						tjEntity.setHt1(tjEntity.getHt1()+ht.getHtje());
					}else if(DateUtils.compare2DateString(ht.getQdrq(), DateUtils.getQ2ndDateStr())<=0) {
						tjEntity.setHt2(tjEntity.getHt2()+ht.getHtje());
					}else if(DateUtils.compare2DateString(ht.getQdrq(), DateUtils.getQ3rdDateStr())<=0) {
						tjEntity.setHt3(tjEntity.getHt3()+ht.getHtje());
					}else if(DateUtils.compare2DateString(ht.getQdrq(), DateUtils.getQ4thDateStr())<=0) {
						tjEntity.setHt4(tjEntity.getHt4()+ht.getHtje());
					}
					person.setXsTjEntity(tjEntity);
				}
			}
		}
		// 项目
		// 循环  将合同分为在途和备案项目，在途项目按照金额分3档，备案项目分重点普通
		for (XsXmEntity xm : xmList) {
			for (XsEntity person : personList) {
				if(person.getUserid().equals(xm.getSqrxm())) {
					XsTjEntity tjEntity = person.getXsTjEntity();
					if(xm.getBarq() == null || "".equals(xm.getBarq())) { // 在途项目
						if(xm.getTzgswy()<500) {
							tjEntity.setHtdown(tjEntity.getHtdown()+1);
						}else if(xm.getTzgswy()<1500) {
							tjEntity.setHtmid(tjEntity.getHtmid()+1);
						}else {
							tjEntity.setHtup(tjEntity.getHtup()+1);
						}
					}else { // 备案项目
						if("0".equals(xm.getXmdj())) { // 重点项目
							tjEntity.setHtimportant(tjEntity.getHtimportant()+1);
						}else {
							tjEntity.setHtnormal(tjEntity.getHtnormal()+1);
						}
					}
					person.setXsTjEntity(tjEntity);
				}
			}
		}
		
		// 投标
		for (XsTbEntity tb : tbList) {
			for (XsEntity person : personList) {
				if(person.getUserid().equals(tb.getSqr())) { // 有一次投标数加1
					XsTjEntity tjEntity = person.getXsTjEntity();
					tjEntity.setTbcount(tjEntity.getTbcount()+1);
					person.setXsTjEntity(tjEntity);
				}
			}
		}
		// 日报
		int rbCount = 0;
		for (XsRbEntity rb : rbList) {
			for (XsEntity person : personList) {
				if(person.getUserid().equals(rb.getModedatacreater())) { // 有一次日报加1
					XsTjEntity tjEntity = person.getXsTjEntity();
					tjEntity.setKhf(tjEntity.getKhf()-1); // 原本存的考勤数，写了一次日报-1，最终是未写日报的次数
					if(tjEntity.getKhf()<0) {
						tjEntity.setKhf(0);
					}
					person.setXsTjEntity(tjEntity);
				}
			}
		}
		// 回款
		for (XsHkEntity hk : hklist) {
			for (XsEntity person : personList) {
				if(person.getUserid().equals(hk.getFzr())) {
					XsTjEntity tjEntity = person.getXsTjEntity();
					if(DateUtils.compare2DateString(hk.getSqrq(), year+"-01-01")<0) {
						tjEntity.setHtpre(tjEntity.getHtpre()+hk.getJzje());
					}else {
						tjEntity.setHtcur(tjEntity.getHtcur()+hk.getJzje());
					}
					person.setXsTjEntity(tjEntity);
				}
			}
			
		}
	}	
	
	private void sumHj(ExportXsEntity tongji, ExportXsEntity exportParentXsEntity) {
		tongji.setHtpre(tongji.getHtpre() + exportParentXsEntity.getHtpre());
		tongji.setHtcur(tongji.getHtcur() + exportParentXsEntity.getHtcur());
		tongji.setHte(tongji.getHte() + exportParentXsEntity.getHte());
		tongji.setHt1(tongji.getHt1() + exportParentXsEntity.getHt1());
		tongji.setHt2(tongji.getHt2() + exportParentXsEntity.getHt2());
		tongji.setHt3(tongji.getHt3() + exportParentXsEntity.getHt3());
		tongji.setHt4(tongji.getHt4() + exportParentXsEntity.getHt4());
		tongji.setXj(tongji.getXj() + exportParentXsEntity.getXj());
		tongji.setZtSbs(tongji.getZtSbs() + exportParentXsEntity.getZtSbs());
		tongji.setHtdown(tongji.getHtdown() + exportParentXsEntity.getHtdown());
		tongji.setHtmid(tongji.getHtmid() + exportParentXsEntity.getHtmid());
		tongji.setHtup(tongji.getHtup() + exportParentXsEntity.getHtup());
		tongji.setZtXj(tongji.getZtXj() + exportParentXsEntity.getZtXj());
		tongji.setBaSbs(tongji.getBaSbs() + exportParentXsEntity.getBaSbs());
		tongji.setHtnormal(tongji.getHtnormal() + exportParentXsEntity.getHtnormal());
		tongji.setHtimportant(tongji.getHtimportant() + exportParentXsEntity.getHtimportant());
		tongji.setBaXj(tongji.getBaXj() + exportParentXsEntity.getBaXj());
		tongji.setTbcount(tongji.getTbcount() + exportParentXsEntity.getTbcount());
		tongji.setTbXj(tongji.getTbXj() + exportParentXsEntity.getTbXj());
		tongji.setKhf(tongji.getKhf() + exportParentXsEntity.getKhf());
		tongji.setZf(tongji.getZf() + exportParentXsEntity.getZf());
	}
	private ExportXsEntity changeData2ExportData(XsEntity xs,String id) {
		ExportXsEntity exportXsEntity = new ExportXsEntity();
		exportXsEntity.setId(id);
		exportXsEntity.setDeptname(xs.getDeptname());
		exportXsEntity.setCompanystartdate(xs.getCompanystartdate());
		exportXsEntity.setLastname(xs.getLastname());
		exportXsEntity.setHtpre(xs.getXsTjEntity().getHtpre()/10000);
		exportXsEntity.setHtcur(xs.getXsTjEntity().getHtcur()/10000);
		exportXsEntity.setHt1(xs.getXsTjEntity().getHt1()/10000);
		exportXsEntity.setHt2(xs.getXsTjEntity().getHt2()/10000);
		exportXsEntity.setHt3(xs.getXsTjEntity().getHt3()/10000);
		exportXsEntity.setHt4(xs.getXsTjEntity().getHt4()/10000);
		exportXsEntity.setHtdown(xs.getXsTjEntity().getHtdown());
		exportXsEntity.setHtmid(xs.getXsTjEntity().getHtmid());
		exportXsEntity.setHtup(xs.getXsTjEntity().getHtup());
		exportXsEntity.setHtnormal(xs.getXsTjEntity().getHtnormal());
		exportXsEntity.setHtimportant(xs.getXsTjEntity().getHtimportant());
		exportXsEntity.setTbcount(xs.getXsTjEntity().getTbcount());
		
		exportXsEntity.setHte(exportXsEntity.getHt1()+exportXsEntity.getHt2()+exportXsEntity.getHt3()+exportXsEntity.getHt4());//全年合同额
		if(exportXsEntity.getHte()==0) {
			exportXsEntity.setHkl(0);//今年回款率
		}else {
			exportXsEntity.setHkl(exportXsEntity.getHtcur()/exportXsEntity.getHte());//今年回款率
		}
		
		exportXsEntity.setXj(exportXsEntity.getHte()/100);//合同得分_小计
		exportXsEntity.setZtSbs(xs.getXsTjEntity().getHtdown()+xs.getXsTjEntity().getHtmid()+xs.getXsTjEntity().getHtup());//总共在途项目分_申报数
		exportXsEntity.setZtXj(xs.getXsTjEntity().getHtdown()+xs.getXsTjEntity().getHtmid()*2+xs.getXsTjEntity().getHtup()*5);
		exportXsEntity.setBaSbs(xs.getXsTjEntity().getHtnormal()+xs.getXsTjEntity().getHtimportant());//合同 - 备案  申报数
		exportXsEntity.setBaXj(xs.getXsTjEntity().getHtnormal()*3+xs.getXsTjEntity().getHtimportant()*6);//备案项目_小计
		exportXsEntity.setTbXj(xs.getXsTjEntity().getTbcount()*5);//投标数_小计
		exportXsEntity.setKhf(xs.getXsTjEntity().getKhf());//日常考核分   缺少日报天数*2+缺少出差天数*3
		exportXsEntity.setZf(exportXsEntity.getXj()+exportXsEntity.getZtXj()+exportXsEntity.getBaXj()+exportXsEntity.getTbXj()+exportXsEntity.getKhf());
		return exportXsEntity;
	}
	
	private XsTjEntity sumTj(XsTjEntity xstjFinal,XsTjEntity xstj) {
		XsTjEntity retEntity = new XsTjEntity();
		retEntity.setHtpre(xstjFinal.getHtpre()+xstj.getHtpre());
		retEntity.setHtcur(xstjFinal.getHtcur()+xstj.getHtcur());
		retEntity.setHt1(xstjFinal.getHt1()+xstj.getHt1());
		retEntity.setHt2(xstjFinal.getHt2()+xstj.getHt2());
		retEntity.setHt3(xstjFinal.getHt3()+xstj.getHt3());
		retEntity.setHt4(xstjFinal.getHt4()+xstj.getHt4());
		retEntity.setHtdown(xstjFinal.getHtdown()+xstj.getHtdown());
		retEntity.setHtmid(xstjFinal.getHtmid()+xstj.getHtmid());
		retEntity.setHtup(xstjFinal.getHtup()+xstj.getHtup());
		retEntity.setHtnormal(xstjFinal.getHtnormal()+xstj.getHtnormal());
		retEntity.setHtimportant(xstjFinal.getHtimportant()+xstj.getHtimportant());
		retEntity.setTbcount(xstjFinal.getTbcount()+xstj.getTbcount());
		retEntity.setKhf(xstjFinal.getKhf()+xstj.getKhf());
		return retEntity;
	}
	
	private void calcXsData_bak(XsEntity person,
			List<XsHtEntity> htList,List<XsXmEntity> xmList,List<XsRbEntity> rbList,
			List<XsTbEntity> tbList,List<XsHkEntity> hklist,
			String year,List<XsBkhrEntity> bkhrList,Map<String, Integer> wqdkMap,int yccDays){
		XsTjEntity tjEntity = new XsTjEntity();
		// 合同
		// 循环 将合同分成1-4季度 金额分别累加起来
		for (XsHtEntity ht : htList) {
			if(person.getUserid().equals(ht.getSqr())) {
				if(DateUtils.compare2DateString(ht.getQdrq(), DateUtils.getQ1thDateStr())<=0) {
					tjEntity.setHt1(tjEntity.getHt1()+ht.getHtje());
				}else if(DateUtils.compare2DateString(ht.getQdrq(), DateUtils.getQ2ndDateStr())<=0) {
					tjEntity.setHt2(tjEntity.getHt2()+ht.getHtje());
				}else if(DateUtils.compare2DateString(ht.getQdrq(), DateUtils.getQ3rdDateStr())<=0) {
					tjEntity.setHt3(tjEntity.getHt3()+ht.getHtje());
				}else if(DateUtils.compare2DateString(ht.getQdrq(), DateUtils.getQ4thDateStr())<=0) {
					tjEntity.setHt4(tjEntity.getHt4()+ht.getHtje());
				}
			}
		}
//		log.info("{}:季度1:{},2:{},3:{},4:{}",person.getLastname(),
//				tjEntity.getHt1(),tjEntity.getHt2(),tjEntity.getHt3(),tjEntity.getHt4());
		// 项目
		// 循环  将合同分为在途和备案项目，在途项目按照金额分3档，备案项目分重点普通
		for (XsXmEntity xm : xmList) {
//			if(xm.getXmmc().contains("河南晋开集团")) {
//				log.info("=================="+xm.getTzgswy()+",,,"+person.getLastname()+",,,"+xm.getSqrxm()+"---"+person.getUserid()+"==="+person.getLastname()+",zt:"+xm.getZt()+",barq:"+xm.getBarq());
//			}
			
			if(person.getUserid().equals(xm.getSqrxm())) {
//				2021.11.8 在途项目获取uf_ztxm 字段zt = 0或1 的 备案项目获取zt = 2 的
//				if(xm.getBarq() == null || "".equals(xm.getBarq())) { // 在途项目
				if("0".equals(xm.getZt()) || "1".equals(xm.getBarq())) { // 在途项目
					if(xm.getTzgswy()<500) {
						tjEntity.setHtdown(tjEntity.getHtdown()+1);
					}else if(xm.getTzgswy()<1500) {
						tjEntity.setHtmid(tjEntity.getHtmid()+1);
					}else {
						tjEntity.setHtup(tjEntity.getHtup()+1);
					}
//				}else { // 备案项目
				}else if("2".equals(xm.getZt())) { // 备案项目
					// 2022.11.9更改重点项目从 项目等级==0 变为判断金额 >1亿是重点项目
//					if("0".equals(xm.getXmdj())) { // 重点项目
//						tjEntity.setHtimportant(tjEntity.getHtimportant()+1);
//					}else {
//						tjEntity.setHtnormal(tjEntity.getHtnormal()+1);
//					}
					if(xm.getTzgswy()>10000) {
						tjEntity.setHtimportant(tjEntity.getHtimportant()+1);
					}else {
						tjEntity.setHtnormal(tjEntity.getHtnormal()+1);
					}
				}
			}
		}
		// 
//		tjEntity.setHtmid(tjEntity.getHtmid()*2);
//		tjEntity.setHtup(tjEntity.getHtup()*5);
//		tjEntity.setHtimportant(tjEntity.getHtimportant()*6);
//		tjEntity.setHtnormal(tjEntity.getHtnormal()*3);
//		log.info("{}:在途1:{},2:{},3:{},重点:{},普通:{}",person.getLastname(),
//				tjEntity.getHtdown(),tjEntity.getHtmid(),tjEntity.getHtup(),tjEntity.getHtimportant(),tjEntity.getHtnormal());
		// 投标
		int tbCount = 0;
		for (XsTbEntity tb : tbList) {
			if(person.getUserid().equals(tb.getSqr())) { // 有一次投标数加1
				tbCount ++;
			}
		}
		tjEntity.setTbcount(tbCount); // 赋值投标数
//		log.info("{}:投标数:{}",person.getLastname(),tbCount);
		// 日报
		String khksrq = "";
		for (XsBkhrEntity bkhr : bkhrList) {
			if(person.getUserid().equals(bkhr.getBkhr())){
				khksrq = bkhr.getKhksrq()==null?"":bkhr.getKhksrq();
				if(khksrq.contains("-")) {
					String khksrqYear = khksrq.split("-")[0];
					int currentYear = Integer.parseInt(year);
					if(Integer.parseInt(khksrqYear)<currentYear) {
						khksrq = currentYear+"-01-01";
					}
				}
			}
		}
		if(!"".equals(khksrq)) {
			int kqCount = xsMapper.queryKQByDate(person.getLoginid(),khksrq)==null?0:xsMapper.queryKQByDate(person.getLoginid(),khksrq);
			int rbCount = 0;
			for (XsRbEntity rb : rbList) {
				if(person.getUserid().equals(rb.getModedatacreater())) { // 有一次日报加1
					rbCount ++;
				}
			}
			int i = rbCount-kqCount > 0?0:rbCount-kqCount;
//			System.out.println(person.getLastname()+"=================================="+person.getUserid());
			int wccDays = wqdkMap.get(person.getUserid()) == null? yccDays:wqdkMap.get(person.getUserid());
//			log.info("{}:查询考勤数:{},提交日报数:{},未出差天数:{},应出差天数:{}",person.getLastname(),kqCount,rbCount,wccDays, yccDays);
			tjEntity.setKhf(i*2 - wccDays*3);// 赋值考核分
		}else {
//			log.info("{}:未在被考核人中查询到数据,直接赋值0",person.getLastname());
			tjEntity.setKhf(0);// 赋值考核分
		}
		
		
		// 回款
		for (XsHkEntity hk : hklist) {
			if(person.getUserid().equals(hk.getFzr())) {
				if(DateUtils.compare2DateString(hk.getSqrq(), year+"-01-01")<0) {
//					tjEntity.setHtpre(tjEntity.getHtpre()+hk.getJzje());
					// 2022.1.21 只需要今年 进账申请流程的 合同数据，根据合同的签订时间（编号的3，4位表示年）判断是否是 今年签订的 还是之前签订的    
				}else if(DateUtils.compare2DateString(hk.getSqrq(), year+"-01-01")>=0 && DateUtils.compare2DateString(hk.getSqrq(), year+"-12-31")<=0){
					if(hk.getGcbh() != null && !"".equals(hk.getGcbh()) && hk.getGcbh().length() >= 4) {
						int hbYear = Integer.parseInt(hk.getGcbh().substring(2,4));
						int simpleYear = Integer.parseInt(year.substring(2,4));
						if(hbYear < simpleYear) {
							tjEntity.setHtpre(tjEntity.getHtpre()+hk.getJzje());
						} else if (hbYear == simpleYear){
							tjEntity.setHtcur(tjEntity.getHtcur()+hk.getJzje());
						}
					}
//					tjEntity.setHtcur(tjEntity.getHtcur()+hk.getJzje());
				}
			}
		}
//		log.info("{}:回款额今年之前:{},今年:{}",person.getLastname(),tjEntity.getHtpre(),tjEntity.getHtcur());
		person.setXsTjEntity(tjEntity); // 统计值赋上
	}

	@Override
	public Boolean wuxiao(String id) {
		// TODO Auto-generated method stub
		return xsMapper.wuxiao(id);
	}

	@Override
	public List<XsTbEntity> tb(String year) {
		// 投标统计
		String deptNamesStr = CommonConfig.getDeptNames();
		log.info("配置文件配置部门信息:{}",deptNamesStr);
		String[] deptNames = deptNamesStr.split(",");
		List<XsTbEntity> tblist = xsMapper.queryXsTbDetail(year,deptNames);
		// 处理投标数据
		for (XsTbEntity tb : tblist) {
			if(tb.getTbnumber() == 0) {
				tb.setSuccessrate(0);
			}else {
				tb.setSuccessrate(tb.getZbnumber()*100/tb.getTbnumber());
			}
			
		}
		HashMap hashMap = new HashMap();
		
		return tblist;
	}	
	
	
	@Autowired
	DeptMapper deptMapper;
	@Autowired
    private XsWQService xsWQService;
	// 查询 年 应出勤天数
	private int queryWorkDaysByYear(String year) {
		return deptMapper.queryWorkDaysByYear(year);
	}
	
	private List<XsWQEntity> getFinallist(String year,int yccDays){
		String deptName = "";
		String beginDate = year+"-01-01";
		String endDate = year+"-12-31";
		Map<String, XsWQEntity> map = new HashMap<String, XsWQEntity>();
		
		List<XsWQEntity> list = xsWQService.queryXsWQData(beginDate,endDate);
		for (XsWQEntity xsWQEntity : list) {
			if(map.get(xsWQEntity.getLoginid()) == null) {
				Map<String, Map<String,Set<String>>>  pmap = xsWQEntity.getPmcm();
				if(pmap == null) {
					pmap = new HashMap<String, Map<String,Set<String>>>();
				}
				
				Map<String,Set<String>> cityMap = pmap.get(xsWQEntity.getProvince());
				if (cityMap == null) {
					cityMap = new HashMap<String,Set<String>>();
					Set<String> timeSet = new HashSet<String>();
					timeSet.add(xsWQEntity.getOperatedate());
					cityMap.put(xsWQEntity.getCity(),timeSet);
				}else {
					Set<String> timeSet = new HashSet<String>();
					if(cityMap.get(xsWQEntity.getCity()) != null) {
						timeSet = cityMap.get(xsWQEntity.getCity());
					}
					timeSet.add(xsWQEntity.getOperatedate());
					cityMap.put(xsWQEntity.getCity(), timeSet);
				}
				pmap.put(xsWQEntity.getProvince(), cityMap);
				xsWQEntity.setPmcm(pmap);
				Map<String,Map<String,Integer>> pccountMap = xsWQEntity.getPccount();
				if(pccountMap == null) {
					pccountMap = new HashMap<String, Map<String,Integer>>();
				}
				Map<String,Integer> countMap = pccountMap.get(xsWQEntity.getProvince());
				if(countMap == null) {
					countMap = new HashMap<String, Integer>();
				}
				for (String key : cityMap.keySet()) {
					countMap.put(key, cityMap.get(key).size());
				}
				int cityCounts = 0;
				int ccdays = 0;
				for (String province : pccountMap.keySet()) {
					Map<String,Integer> cm = pccountMap.get(province);
					cityCounts += cm.size();
					for (String city : cm.keySet()) {
						ccdays += cm.get(city);
					}
				}
				pccountMap.put(xsWQEntity.getProvince(), countMap);
				xsWQEntity.setPccount(pccountMap);
				xsWQEntity.setProvinceCount(pccountMap.size()); // 出差省份个数
				xsWQEntity.setCityCount(cityCounts); // 出差地级市个数
				xsWQEntity.setCcDays(ccdays); // 出差天数
				map.put(xsWQEntity.getLoginid(), xsWQEntity);
			}else {
				XsWQEntity old = map.get(xsWQEntity.getLoginid());
				Map<String, Map<String,Set<String>>>  pmap = old.getPmcm();
				if(pmap == null) {
					pmap = new HashMap<String, Map<String,Set<String>>>();
				}
				Map<String,Set<String>> cityMap = pmap.get(xsWQEntity.getProvince());
				if (cityMap == null) {
					cityMap = new HashMap<String,Set<String>>();
					Set<String> timeSet = new HashSet<String>();
					timeSet.add(xsWQEntity.getOperatedate());
					cityMap.put(xsWQEntity.getCity(),timeSet);
				}else {
					Set<String> timeSet = new HashSet<String>();
					if(cityMap.get(xsWQEntity.getCity()) != null) {
						timeSet = cityMap.get(xsWQEntity.getCity());
					}
					timeSet.add(xsWQEntity.getOperatedate());
					cityMap.put(xsWQEntity.getCity(), timeSet);
				}
				pmap.put(xsWQEntity.getProvince(), cityMap);
				xsWQEntity.setPmcm(pmap);
				Map<String,Map<String,Integer>> pccountMap = old.getPccount();
				if(pccountMap == null) {
					pccountMap = new HashMap<String, Map<String,Integer>>();
				}
				Map<String,Integer> countMap = pccountMap.get(xsWQEntity.getProvince());
				if(countMap == null) {
					countMap = new HashMap<String, Integer>();
				}
				for (String key : cityMap.keySet()) {
					countMap.put(key, cityMap.get(key).size());
				}
				int cityCounts = 0;
				int ccdays = 0;
				for (String province : pccountMap.keySet()) {
					Map<String,Integer> cm = pccountMap.get(province);
					cityCounts += cm.size();
					for (String city : cm.keySet()) {
						ccdays += cm.get(city);
					}
				}
				pccountMap.put(xsWQEntity.getProvince(), countMap);
				xsWQEntity.setPccount(pccountMap);
				xsWQEntity.setProvinceCount(pccountMap.size()); // 出差省份个数
				xsWQEntity.setCityCount(cityCounts); // 出差地级市个数
				xsWQEntity.setCcDays(ccdays); // 出差天数
				map.put(xsWQEntity.getLoginid(), xsWQEntity);
			}
		}
		String deptNamef = deptName;
        return map.values().stream().filter((e) -> checkValue(e,deptNamef))
        		.map(xsEntity ->{
        			List<String> pcddList = xsEntity.getPcdds();// 排除地点
		        	xsEntity.setYcqts(yccDays);
		        	if(yccDays == 0) {
		        		xsEntity.setCcl(0);
		        	}else {
		        		xsEntity.setCcl(((int)xsEntity.getCcDays()*100*100/yccDays)/(double)100);
		        	}
		        	StringBuffer sb = new StringBuffer();
		        	Map<String,Map<String,Integer>> pc = xsEntity.getPccount();
		        	for (String province : pc.keySet()) {
						Map<String,Integer> cm = pc.get(province);
						sb.append(province).append("\n\t");
						for (String city : cm.keySet()) {
							sb.append("    ").append(city).append(":").append(cm.get(city)).append("\n\t");
							if(pcddList.toString().contains(city)) { // 如果排除地点包含该地点，减去该城市出差天数
								xsEntity.setCcDays(xsEntity.getCcDays() - cm.get(city));// 出差天数重新计算
								if(xsEntity.getCcDays()<0) {
					        		xsEntity.setCcDays(0);
					        	}
								// 重新计算下出差率
								if(yccDays == 0) {
					        		xsEntity.setCcl(0);
					        	}else {
					        		xsEntity.setCcl(((int)xsEntity.getCcDays()*100*100/yccDays)/(double)100);// 出差率重新计算
					        	}
							}
						}
					}
		        	
		        	xsEntity.setShowMsg(sb.toString());
		        	return xsEntity;
		        	}).collect(Collectors.toList());
    }
	
	private boolean checkValue(XsWQEntity xsEntity,String deptNamef) {
		if("".equals(deptNamef) || deptNamef.equals(xsEntity.getDeptname())) {
    		return true;
    	}else {
    		return false;
    	}
	}
}

